#!/usr/bin/env python
# -*- coding: utf-8-*-

# ETHUSDT 30min 趋势策略

from binance.client import BaseClient
from yuboadmin.trade.utils import Tidata
from yuboadmin.trade.mybinance.base import Futures
from yuboadmin.trade.utils import utility

class TrendStrategy(object):

    def __init__(self, api_key=None, api_secret=None):
    self.api_key = api_key
    self.api_secret = api_secret

    """
        1.修改双向持仓
        2.修改U本位杠杆
        3.获取仓位余额

        30min趋势策略
        参数 交易对
    """
    def todo30min(self,symbol,interval):
        # 修改U本位杠杆倍数 为10倍
        #Futures.change_futures_leverage(symbol,10)
        # 获取账户余额
        #account = Futures.get_futures_account_balance(client,"BUSD")
        trend = TrendStrategy.calc_trend(self,symbol,interval)
        return trend


    """
        计算ema趋势 参数:交易对
    """
    def calc_trend(self,symbol,interval):
        #趋势 0:无 1:多 2:空
        trend = 0
        #ema系数
        proportion = 0.00005
        #ema日期
        emadays = 39
        #时间周期
        #默认30min
        interval_time = BaseClient.KLINE_INTERVAL_30MINUTE
        if interval == '15min':
            interval_time = BaseClient.KLINE_INTERVAL_15MINUTE
        elif interval == '30min':
            interval_time = BaseClient.KLINE_INTERVAL_30MINUTE
        elif interval == '1h':
            interval_time = BaseClient.KLINE_INTERVAL_1HOUR
        else :
            interval_time = BaseClient.KLINE_INTERVAL_30MINUTE

        #可能会增加小数点位数

        #获取30min K线数据
        kline_30min = Futures.futures_klines(self,symbol,interval_time)
        ema36_30min = Tidata.compute_futures_kline_ema(kline_30min,emadays)
        #k线价格
        price = round(ema36_30min[-1],2)

        #ema36上一根价格
        price_ema_1 = round(ema36_30min[-2],2)
        #ema36上上一根价格
        price_ema_2 = round(ema36_30min[-3],2)
        #ema x 系数
        price_proportion = round(price_ema_1 * proportion)

        if price_ema_1 - price_proportion > price_ema_2 and trend == 0:
            trend = 1 #多单趋势
        elif price_ema_1 + price_proportion < price_ema_2 and trend == 0:
            trend = 2 #空单趋势
        else :
            trend = 0 #没有趋势

        return trend

class TdStrategy(object):

    def __init__(self, api_key=None, api_secret=None):
        self.api_key = api_key
        self.api_secret = api_secret

    """
    计算kline td序列
    """
    def calc_td_sequence(self,symbol,interval):
        #计算kline周期
        interval_time = utility.kline_interval(interval)
        #获取kline
        klinedata = Futures.futures_klines(self,symbol,interval_time)
        #返回td序列
        return Tidata.td_sequence(klinedata)

    """
        td策略 日线ema
        msg 0无 1多 2空
    """
    def calc_tdema(self,symbol,ema1,ema2):
        #计算kline周期
        interval_time = BaseClient.KLINE_INTERVAL_1DAY
        #获取kline
        klinedata = Futures.futures_klines(self,symbol,interval_time)

        #ema1 data
        ema1data = Tidata.compute_futures_kline_ema(klinedata,ema1)
        #ema1 上一根价格
        price_ema1 = round(ema1data[-2],2)
        #ema2 data
        ema2data = Tidata.compute_futures_kline_ema(klinedata,ema2)
        #ema2 上一根价格
        price_ema2 = round(ema2data[-2],2)

        msg = 0
        if price_ema1 < price_ema2:
            msg = 2
        elif price_ema1 > price_ema2:
            msg = 1
        return msg

    """
        td策略 定时任务
    """
    def td_seq_msg(self,symbol,interval,ema1,ema2):
        # #日线趋势
        # ematrend = TdStrategy.calc_tdema(self,symbol,ema1,ema2)
        # #td序列
        # tdseq = TdStrategy.calc_td_sequence(self,symbol,interval)
        # tdseq_now = tdseq[12]
        # info = 'not'
        # if interval == '30min':
        #     if tdseq_now  == '-9' and ematrend == 1:
        #         info = 'long'
        #     elif tdseq_now  == '9' and ematrend == 2:
        #         info = 'short'
        # else:
        #     info = 'not'
        # elif interval == '1h':
        #         if tdseq_now == '-8' and ematrend == 1:
        #      info = 'long'
        #         elif tdseq_now == '7' and ematrend == 2:
        # info = 'short'
        #         else:
        # info = 'not'
        # elif interval == '2h':
        #         if tdseq_now == '-9' and ematrend == 1:
        # info = 'long'
        # elif tdseq_now == '9' and ematrend == 2:
        # info = 'short'
        #         else:
        # info = 'not'
        #         else:
        # info = 'not'
        #   return info